// pages/follow/follow.js
const app = getApp()
var help = require('../../utils/help.js');
var login = require('../../utils/login.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageUrl: app.data.imageUrl,

    data: [],
    page: 1, // 当前页
    is_load: true, // 是否继续加载
    load_lock: false, // 加载锁
    load_ts: '',
    keyword: '',

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.loadData(true);
  },

  /**
   * 加载数据
   */
  loadData: function (is_one) {
    var that = this;

    if (is_one === true) {
      that.setData({
        is_load: true
      })
    }
    if (this.data.load_lock || !this.data.is_load) {
      return false;
    }
    this.setData({
      load_lock: true,
      load_ts: '正在加载'
    })
    if (is_one === true) {
      that.setData({
        page: 1,
        data: []
      })
    }

    var url = app.data.apiUrl + 'Yisheng/guanzhu';
    var data = {
      token: app.data.userInfo ? app.data.userInfo.token : '',
      keyword: that.data.keyword,
      page: that.data.page,
    };
    help.get(url, data, that, function (res, that) {
      that.setData({
        load_lock: false
      })
      var result = res.data;
      console.log(result);
      if (result.code == 1) {
        if (result.data.length > 0) {
          if (is_one === true) {
            var new_data = result.data;
          } else {
            var new_data = that.data.data.concat(result.data)
          }
          console.log(new_data)
          that.setData({
            data: new_data,
            load_ts: ''
          })
        } else {
          if (is_one === true) {
            that.setData({
              data: [],
            })
          }
          that.setData({
            is_load: false,
            load_ts: '没有更多数据了',

          })
        }
        that.data.page++;

      } else if (result.code == 401) {
        help.msg('登录已失效，正在重新登录...')
        // 重新检测登录状态
        login.getUserInfo(app);
        return false;
      } else {
        help.msg(result.msg)
        return false;
      }

    });
  },

  //取消关注
  guanzhu: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;

    wx.showModal({
      title: '提示',
      content: '确定要取消关注吗？',
      success: function (res) {
        if (res.confirm) {
          var url = app.data.apiUrl + '/Yisheng/sc';
          wx.showLoading({
            title: '正在加载...',
          })
          var data = {
            token: app.data.userInfo ? app.data.userInfo.token : '',
            id: id,
            doc_id: that.data.data.id
          };
          help.get(url, data, that, function (res, that) {
            wx.hideLoading()
            var result = res.data;
            console.log(result);
            if (result.code == 1) {
              help.msg('取消关注成功');
              that.loadData(true);
            } else if (result.code == 1) {
              help.msg('登录已失效，正在重新登录...')
              // 重新检测登录状态
              login.getUserInfo(app);
            } else {
              help.msg(result.msg)
              return false;
            }


          }, function (res, that) {
            wx.hideLoading()
            console.log(res);
            help.msg('网络异常！')
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.loadData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})